﻿using CMS.Dialogs;
using CMS.Forms;
using CMS.REST;
using CMS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace CMS
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //for(int i=0;i<1000;i++)
            //Debug.WriteLine( Utils.GetComputerHash(RESTClient.GetServerTimeStamp()));



            //RESTResponse authKeyResp = RESTClient.HttpPost("apis/v1/login", new JObject() { { "username", "admin" }, { "password", Utils.MD5("admin") } });
            //if (authKeyResp.IsSuccess)
            //{
            //    string authKey = authKeyResp.Data[0].ToString();
            //    RESTClient.SetAuthKey(authKey);
            //    Debug.WriteLine(authKey);
            //}

            #region 测试
            Permission.RefreshPermission();
            //Application.Run(new Form1());
            //Application.Run(new MainForm());

            #endregion

            #region 登陆
            LoginDialog loginDialog = LoginDialog.GetInstance();
            RESTClient.OpeningRelogin += (sender, e) =>
            {
                if (MessageBox.Show(loginDialog, "您的账号在另一地点登陆,您已被迫下线,是否尝试重新登陆", "下线通知", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            };
            RESTClient.ShouldRelogin += (sender, e) => loginDialog.ShowDialog();

            if (loginDialog.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new MainForm());
            }
            #endregion
        }
    }
}
